<?php include("siteTop.php"); ?>
<?php include("siteMiddle.php"); ?>

<script src="js/jquery-1.6.2.min.js"></script>

    <!--ACTUAL JS SCRIPTS AND JQUERIES-->
<script type="text/javascript">
      var incomingDocumentId=new Array(); 
      var incomingDocumentType=new Array(); 
      var documentsOnHandId=new Array(); 
      var documentsOnHandType=new Array(); 
      var documentsOnHandStatus=new Array(); 
      var MAXincomingDocuments;
      var MAXdocumentsOnHand;
      jQuery(document).ready(function(){ 
        // CHANGE FIRST THE CSS OF THE NAV BAR
        jQuery("#dashboard-nav").addClass("highlightMenuBar");
  
         jQuery("#documentsToReceiveListTable").hide();
         jQuery("#documentsOnHandListTable").hide();
         jQuery("#taggedDocumentsTable").hide();
         jQuery(".documentsToReceiveFormClass").hide();
         jQuery(".documentsOnHandFormClass").hide();
         
         //AJAX for Incoming documents
         jQuery.ajax({
                type : 'POST',
                url : 'ajaxRequests/getDocumentsForDashboard.php',
                dataType : 'json',
                data: {
                    user : "<?php echo $userId; ?>",
                    action:  "action = 'Sent'",
                    receiveFlag: "( receiveFlag = 'Pending' AND orgFlag = 'Internal')"
                },
                success : function(data){
                    
                    if(data){
                       
                        
                        MAXincomingDocuments=0;
                        jQuery("#documentsToReceiveListTable").show();
                        jQuery(".documentsToReceiveFormClass").show();
                        jQuery.each(data, function(index, data) {
                            incomingDocumentId[MAXincomingDocuments] = data.documentId;
                            incomingDocumentType[MAXincomingDocuments] = data.type;
                            jQuery("#documentsToReceiveListTable").append("<tr class='incoming"+data.type+"'> <td style='font-size:12px'>"+data.tag+"</td><td style='font-size:12px'>"+data.documentSetupId+"</td><td style='font-size:12px'>"+data.name+"</td>  </tr>");
                            MAXincomingDocuments++;
                        });    
                        
                    }
                    else{
                        jQuery("#documentsToReceiveList").append("<i> No Documents Available </i>");
                    }
                       
                    
                    
                   
                },
                error : function() {
                    alert("error ajax get incoming documents by user");
                }
            }); // END - displays all incoming documents by user 
 
         //AJAX for documents on hand
         jQuery.ajax({
                type : 'POST',
                url : 'ajaxRequests/getDocumentsForDashboard.php',
                dataType : 'json',
                data: {
                    user : "<?php echo $userId; ?>",
                    action:  "(action = 'Received' OR action = 'Pending' OR action = 'Cancelled')",
                    receiveFlag: "((receiveFlag = 'Received' AND orgFlag = 'Internal') OR (receiveFlag= 'Pending' AND orgFlag = 'External'))"
                },
                success : function(data){
                    
                    if(data){
                       
                        MAXdocumentsOnHand=0;
                      jQuery("#documentsOnHandListTable").show();
                      jQuery(".documentsOnHandFormClass").show();
                        jQuery.each(data, function(index, data) {
                            documentsOnHandId[MAXdocumentsOnHand]=data.documentId;
                            documentsOnHandType[MAXdocumentsOnHand]=data.type;
                            documentsOnHandStatus[MAXdocumentsOnHand]=data.status;
                            jQuery("#documentsOnHandListTable").append("<tr class='onHand"+data.type+"'> <td style='font-size:12px'>"+data.tag+"</td><td style='font-size:12px'>"+data.documentSetupId+"</td><td style='font-size:12px'>"+data.name+"</td>  </tr>");
                            MAXdocumentsOnHand++;
                        });    
                    }
                    else{
                        jQuery("#documentsOnHandList").append("<i> No Documents Available </i>");
                    }
                       
                    
                    
                   
                },
                error : function() {
                    alert("error ajax get documents on by user");
                }
            }); // END - displays all incoming documents by user 

          //AJAX FOR TAGGED By DOCUMENTS LIST
            jQuery.ajax({
                type : 'POST',
                url : 'ajaxRequests/getTaggedMainDocument.php',
                dataType : 'json',
                data: {
                    origin : "<?php echo $userId; ?>"
                },
                success : function(data){
                    
                    if(data){
                        jQuery("#taggedDocumentsTable").show();
                          jQuery("#taggedDocumentsTable").before('<i style="font-size:12px"> Note: click the ID to view the document </i>');
                        jQuery.each(data, function(index, data) {
                            jQuery("#taggedDocumentsTable").append("<tr> <td><a href='#' class='taggedByHREF' id='"+data.mainDocumentId+"' >"+data.mainDocumentId + "</a></td> <td> "+data.tag+" </td> <td>"+data.documentSetupId +" </td>");
                        });    
                    }
                    else{
                        jQuery("#taggedDocuments").append("<i> No Documents Available </i>");
                    }
                       
                    
                    
                   
                },
                error : function() {
                    alert("error ajax getTaggedMainDocument");
                }
            }); // END - displays all tagged by user documents
        


            jQuery(".taggedByHREF").live("click",function(){
              flag=0;
              clicked = jQuery(this).attr("id");
              for(x=0;x < MAXincomingDocuments && !flag; x++){
                  if(clicked == incomingDocumentId[x]){
                      flag=1;
                  }
              }
              
              if(flag)
              {
                 jQuery.ajax({
                            type : 'POST',
                            url : 'ajaxRequests/makeDocumentPendingAjax.php',
                            dataType : 'json',
                            data: {
                                documentId : clicked,
                                location : "<?php echo $userId ?>"   
                            }
                            , success : function(data){
                                jQuery("#formTaggedDocuments").attr("action","viewPageSending.php");
                            } ,
                            error : function(){
                                alert("Error in ajax makeDocumentPendingAjax");
                            }

                 });
                 
                 
           
              }
              else{
                  for(x=0;x < documentsOnHandId.length && !flag; x++){
                      if(clicked == documentsOnHandId[x]){
                          flag=1;
                      }
                  }
                  x--;
                  if(flag)
                  {
                       if(documentsOnHandStatus[x]=='Received')
                       {
                           jQuery("#formTaggedDocuments").attr("action","viewPageSending.php");
                           
                       }
                       else{
                           jQuery("#formTaggedDocuments").attr("action","viewPageReceiving.php");
                        
                       }
                  }
              }
              jQuery("#inputTaggedDocuments").val(clicked);
              jQuery("#formTaggedDocuments").submit();
            }); // END - jquery taggedbyhref.live("click");
            
            jQuery(":text").live("click",function(){
               jQuery(this).val(''); 
                
            });
            
           
           
        // DOCUMENTS TO RECEIVE ON BLUR   
           jQuery("#documentsToReceive").live( "blur", function() {
               search=jQuery(this).val();
               flag=0;
               x=0; 
               for(x=0;x < MAXincomingDocuments && !flag ;x++){

                   if(search == incomingDocumentId[x]){
                       flag=1;
                   }
                   
                  
               }
               x--;
               
               if(flag){
                   if(incomingDocumentType[x] == "MainDocument"){
                       jQuery("#formDocumentsToReceive").attr("action","viewPageReceiving.php");
                       
                       
                        jQuery.ajax({
                            type : 'POST',
                            url : 'ajaxRequests/makeDocumentPendingAjax.php',
                            dataType : 'json',
                            data: {
                                documentId : search,
                                location : "<?php echo $userId ?>"   
                            },
                            success : function(data){
                                jQuery("#formDocumentsToReceive").submit();
                            }, error : function(){
                                alert("Error ajax in makedocumentPendingAjax")
                            }


                        });
                       
                       
                       
                   }
                   else{
//ATTACHMENT RECEIVING!!!!!!                       
                       jQuery("#formDocumentsToReceive").attr("action","viewPageAttachment.php");
                       
                       jQuery.ajax({
                            type : 'POST',
                            url : 'ajaxRequests/receiveAttachmentAjax.php',
                            dataType : 'json',
                            data: {
                                documentId : search,
                                location : "<?php echo $userId ?>"   
                            }, success :function(data){
                                jQuery("#formDocumentsToReceive").submit();
                            }, error : function(){
                                alert("error in ajax receiveattachmentajax");
                            }


                        });
                       
                       
                       
                       
                   }
               }
               // Error handlings!    
               
               else if(jQuery(this).val() == '')
               {
                   jQuery(this).val('');
               }
               else{
                   alert(search +" is not on your incoming documents");
                   jQuery(this).val('');
               }
                   
               
               
            });
            
            jQuery("#documentsToReceiveButton").click(function(){
                
            
                jQuery("#documentsToReceive").blur();
            });
            
            
            
            jQuery("#documentsOnHand").blur( function() {
              search=jQuery(this).val();
               flag=0;
               x=0; 
               for(x=0;x < documentsOnHandId.length && !flag ;x++){
                   if(search == documentsOnHandId[x]){
                       flag=1;
                   }
               }
               x--;
               if(flag){
                  
                   if(documentsOnHandType[x] == "MainDocument"){
                       
                       if(documentsOnHandStatus[x] == 'Received'  || documentsOnHandStatus[x] == 'Cancelled' ){
                           jQuery("#formDocumentsOnHand").attr("action","viewPageSending.php");
                           
                       }
                       else{
                           jQuery("#formDocumentsOnHand").attr("action","viewPageReceiving.php");
                       }
                           
                       
                       jQuery("#formDocumentsOnHand").submit();
                   }
                   else{
//ATTACHMENT RECEIVING!!!!!!                       
                       jQuery("#formDocumentsOnHand").attr("action","viewPageAttachment.php");
                       jQuery("#formDocumentsOnHand").submit();
                   
                   }
               }
               // Error handlings!    
               
               else if(jQuery(this).val() == '')
               {
                   jQuery(this).val('');
               }
               else{
                   alert(search +" is not on your documents on hand");
                   jQuery(this).val('');
               }
                   
            });
            
         
            jQuery("#onHandSelect").change(function(){
                if(jQuery("#onHandSelect").val()=="all"){
                    jQuery(".onHandMainDocument").show();
                    jQuery(".onHandAttachment").show();
                }
                else if(jQuery("#onHandSelect").val()=="MainDocuments"){
                    jQuery(".onHandMainDocument").show();
                    jQuery(".onHandAttachment").hide();
                }
                else{
                    jQuery(".onHandMainDocument").hide();
                    jQuery(".onHandAttachment").show();
                }
                    
                  
                
            });
         jQuery("#incomingSelect").change(function(){
                if(jQuery("#incomingSelect").val()=="all"){
                    jQuery(".incomingMainDocument").show();
                    jQuery(".incomingAttachment").show();
                }
                else if(jQuery("#incomingSelect").val()=="MainDocuments"){
                    jQuery(".incomingMainDocument").show();
                    jQuery(".incomingAttachment").hide();
                }
                else{
                    jQuery(".incomingMainDocument").hide();
                    jQuery(".incomingAttachment").show();
                }
                    
                  
                
            });
            jQuery(".messageBox").hide();
            
            
            <?php if (isset($_POST['successMessages'])){ ?>
            jQuery("#successMessages").html("<?php echo $_POST['successMessages']; ?>");
            jQuery(".successMessage").show();
             
            <?php } ?>
            
            jQuery.ajax({
                        type : 'POST',
                        url : 'ajaxRequests/getPendingDocList.php',
                        dataType : 'json',
                        data: {
                            userId : "<?php echo $userId; ?>"
                        },
                        success : function(data){
                             if(data){
                             var PendingMaindoc=new Array(); 
                             var PendingAttachment=new Array(); 
                             var ERRORMessage = "The Following Sent Documents have Missing Attachments:";
                             x = 0;
                             jQuery.each(data, function(index, data) {
                                  PendingMaindoc[x]=data.mainDocumentId;
                                  PendingAttachment[x]=data.documentId;
                                  x++;
                             }); 
                             
                             ERRORMessage+="<br><br>"+ PendingMaindoc[0]+" ( "+PendingAttachment[0];
                             for(x=1;x < PendingMaindoc.length ;x++)
                             {
                                if(PendingMaindoc[x] == PendingMaindoc[x-1])
                                {
                                   ERRORMessage+= ", " +PendingAttachment[x] ;
                                }
                                else
                                {
                                   ERRORMessage+=" )<br> " +PendingMaindoc[x]+" ( "+PendingAttachment[x];
                                }    
                                
                             }
                             ERRORMessage += " )";
                             jQuery("#errorMessages").html(ERRORMessage);
                             jQuery(".errorMessage").show();
                             }
                            
                        },
                        error : function() {
                            alert("fail ajax get document name in theatre");
                        }
                    });
            
             
   
    
            jQuery(":text").keypress(function(e){
                   if(e.keyCode == 13) {
                        return false;
                   }
                   
                });;
        });
        
</script>
    <div id="content">
        <h1>Dashboard</h1>
        
        <div class="contentbox">  
            
        <?php
        // Check permission
        $thePermissions = array('VIEWMAIN', 'VIEWATTCH');
        if ( checkPermission( $thePermissions, $userId ) == true ) {
        ?>
            
            <div class="messageBox successMessage">
                <div id="successMessages"> </div>
            </div>

            <div class="messageBox errorMessage" style="border: 2px solid red">
                <div id="errorMessages"> </div>
            </div>
<!--       //INCOMING DOCUMENTS BOX-->
            <div class="dashboardbox">
                <span class="screenlet" style="text-align:center;padding-left : 0; padding-right : 0; padding-top : 0;width:100%" >Incoming Documents</span>
                <div class="contentText" > 
                    Filter:<select id ="incomingSelect">
                        <option value="all">All</option>
                        <option value="MainDocuments">Main Documents</option>
                        <option value="Attachments">Attachments</option>
                    </select>   
                </div>
                    <hr style="margin-bottom:2%;margin-top:2%;">
                    
                    <div class ="contentText"id="documentsToReceiveList" style="margin-right:5%;height:60%;overflow:auto;">
                        <table id="documentsToReceiveListTable"class="documentList" style="font-size:12px; border-top-color:gray; border-top-width:1px " border="1">
                  
                        <tr> <th> Label </th> <th> Type </th> <th> Origin </th></tr> 
                        
                        
               
                    </table> 
                        
                    </div>
                
                <hr>
                <div class="contentText"> 
                    <form id="formDocumentsToReceive" name="formDocumentsToReceive" method="Post" action="viewPageReceiving.php">
                        <i class="documentsToReceiveFormClass" style="font-size:12px;">Note: Input document ID</i> 
                        
                        <input class="documentsToReceiveFormClass"  style="width:90%;height:40px;font-size:30px;" id="documentsToReceive" name="documentsToReceive" value="" type="text"> 
                    </form>
<!--                    <input type="button" style="float:right; margin-right:10% ;margin-top:2%;"id="documentsToReceiveButton" class="siteGreenButton documentsToReceiveFormClass" value="Receive">-->
                </div>
            </div>
       
<!--        Documents On HAND BOX-->
            <div class="dashboardbox">
                <span class="screenlet" style="text-align:center;padding-left : 0; padding-right : 0; padding-top : 0;width:100%" >Documents On Hand</span>
                <div class="contentText" > 
                    Filter:<select id="onHandSelect">
                        <option value="all">All</option>
                        <option value="MainDocuments">Main Documents</option>
                        <option value="Attachments">Attachments</option>
                    </select>   
                    
                </div>
                    <hr style="margin-bottom:2%;margin-top:2%;">
                    
                    <div class ="contentText"id="documentsOnHandList" style="height:60%;overflow:auto;">
                        <table id="documentsOnHandListTable"class="documentList" style="font-size:12px; border-top-color:gray; border-top-width:1px " border="1">
                  
                             <tr> <th> Label </th> <th> Type </th> <th> Origin </th></tr> 
                        
                        
               
                         </table> 
                        
                    </div>
                
                <hr>
                <div class="contentText"> 
                    <form name="formDocumentsOnHand" id="formDocumentsOnHand" method="Post" action=".php">
                       <i class="documentsOnHandFormClass" style="font-size:12px;">Note: Input document ID</i> 
                        <input class="documentsOnHandFormClass"  style="width:90%;height:40px;font-size:30px;" id="documentsOnHand" name="documentsOnHand" value="" type="text"> 
                    </form>
<!--                    <input type="button" style="float:right; margin-right:10% ;margin-top:2%;"id="documentsOnHand" class="siteGreenButton documentsOnHandFormClass" value="View">-->
                </div>
            </div>

<!--      //TAGGED DOCUMENTS BOX-->
            <div class="dashboardbox">
                <span class="screenlet" style="text-align:center;padding-left : 0; padding-right : 0; padding-top : 0;width:100%" >Routed Documents</span>
                
                 <div class="contentText" id="taggedDocuments" style="margin-right:5%;height:80%;overflow:auto;">    
                    
                    <table id="taggedDocumentsTable" class="documentList" style="font-size:12px; border-top-color:gray; border-top-width:1px " border="1">
                  
                        <tr> <th> Id </th> <th> Label </th> <th> Type </th> </tr> 
                       
                       
                       
                    </table> 
                </div>
               
                 
                <div class="contentText"> 
                    <form id="formTaggedDocuments" name="formTaggedDocuments" method="Post" action="viewPageSending.php">
                        <input style="display:none;" id="inputTaggedDocuments"name="taggedDocuments" type="text" > 
                    </form>
                </div>
            </div>

        <?php
        }
        else {
            echo "<div class='noPermissionMsgBox noPermissionMsg' style='border: 2px solid red'><div id='noPermissionMsgs'>You have no permission to view this page.</div></div>";
        }
        // End checking permission
        ?>

        </div> <!-- contentbox end div -->
        <div style="position:relative; visibility: hidden">end</div>
            
    </div> <!-- content end div -->
<?php include ("siteBottom.php"); ?>